//
//  SideMenuViewController.m
//  MFSideMenuDemo
//
//  Created by Michael Frederick on 3/19/12.

#import "SideMenuViewController.h"
#import "MFSideMenu.h"
#import "SlideMenuCell.h"
#import "Settings_VC.h"
#import "ListOfFacts_VC.h"
#import "Favorites_VC.h"
#import "Top_VC.h"
#import "About_VC.h"

@implementation SideMenuViewController

@synthesize sideMenu;

#pragma mark - View Life

- (void) viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor colorWithRed:52.0f/255.0f green:57.0f/255.0f blue:74.0f/255.0f alpha:1.0f];
    
    NSMutableDictionary *tmp = [NSMutableDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"categories" ofType:@"plist"]];
    dataArray = [[NSMutableArray alloc] initWithArray:[tmp objectForKey:@"data"]];
    
    [self performSelector:@selector(select_row) withObject:nil afterDelay:0.3f];
}

-(void)select_row{
   int current_fact_type = [[[[NSUserDefaults standardUserDefaults] objectForKey:@"category"] valueForKey:@"id"] intValue];
    for (int i=0; i<[[[dataArray objectAtIndex:1] objectForKey:@"categories"] count]; i++) {
        int type = [[[[[dataArray objectAtIndex:1]objectForKey:@"categories"] objectAtIndex:i] valueForKey:@"id"] intValue];
        if(current_fact_type==type){
             [self.tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:1] animated:FALSE scrollPosition:UITableViewScrollPositionNone];
            break;
        }
    }  
}

#pragma mark - Table View Data Source

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    NSString *titleString = [[dataArray objectAtIndex:section]objectForKey:@"name"];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 22.0f)];
    imageView.image = [[UIImage imageNamed:@"section_background"] stretchableImageWithLeftCapWidth:0.0f topCapHeight:0.0f];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectInset(imageView.frame, 10.0f, 0.0f)];
    titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12.0f];
    titleLabel.textAlignment = UITextAlignmentLeft;
    titleLabel.textColor = [UIColor colorWithRed:125.0f/255.0f green:129.0f/255.0f blue:146.0f/255.0f alpha:1.0f];
    titleLabel.shadowColor = [UIColor colorWithRed:40.0f/255.0f green:45.0f/255.0f blue:57.0f/255.0f alpha:1.0f];
    titleLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.text = titleString;
    [imageView addSubview:titleLabel];
    
    return imageView;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [[[dataArray objectAtIndex:section] objectForKey:@"categories"] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"Cell";
    
    SlideMenuCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[SlideMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
   
    cell.textLabel.text = [[[[dataArray objectAtIndex:indexPath.section]objectForKey:@"categories"] objectAtIndex:indexPath.row] valueForKey:@"name"];
    cell.imageView.image = [UIImage imageNamed:[[[[dataArray objectAtIndex:indexPath.section]objectForKey:@"categories"] objectAtIndex:indexPath.row] valueForKey:@"icon"]];
    
    return cell;
}


#pragma mark - Table View Delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    
    
    if(indexPath.section==1){
        [[NSUserDefaults standardUserDefaults] setObject:[[[dataArray objectAtIndex:indexPath.section] objectForKey:@"categories"]objectAtIndex:indexPath.row] forKey:@"category"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        ListOfFacts_VC *listOfFactsVC = [[ListOfFacts_VC alloc] initWithNibName:@"ListOfFacts_VC" bundle:nil];
        NSArray *controllers = [NSArray arrayWithObject:listOfFactsVC];
        self.sideMenu.navigationController.viewControllers = controllers;
        
    }
    else if(indexPath.section==2){
        if(indexPath.row==0){
            Settings_VC *settingsVC = [[Settings_VC alloc] initWithNibName:@"Settings_VC" bundle:nil];
            NSArray *controllers = [NSArray arrayWithObject:settingsVC];
            self.sideMenu.navigationController.viewControllers = controllers;
        }
        else{
            About_VC *settingsVC = [[About_VC alloc] initWithNibName:@"About_VC" bundle:nil];
            NSArray *controllers = [NSArray arrayWithObject:settingsVC];
            self.sideMenu.navigationController.viewControllers = controllers;  
        }
     
    }
    else{
        if(indexPath.row==1){
            Favorites_VC *favoritesVC = [[Favorites_VC alloc] initWithNibName:@"Favorites_VC" bundle:nil];
            NSArray *controllers = [NSArray arrayWithObject:favoritesVC];
            self.sideMenu.navigationController.viewControllers = controllers;
        }
        else {
            Top_VC *topVC = [[Top_VC alloc] initWithNibName:@"Top_VC" bundle:nil];
            NSArray *controllers = [NSArray arrayWithObject:topVC];
            self.sideMenu.navigationController.viewControllers = controllers; 
        }
    }
    

    [self.sideMenu setMenuState:MFSideMenuStateHidden];
}


@end
